package services

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
)

/**
* 文件上传--单文件上传
 */
func Upload(c *gin.Context) {
	// 单文件
	file, _ := c.FormFile("file")
	// log.Println(file)
	dst := "./assets/images/" + file.Filename
	// 上传文件至指定的完整文件路径
	c.SaveUploadedFile(file, dst)
	c.String(http.StatusOK, fmt.Sprintf("'%s' uploaded!", file.Filename))
	c.JSON(http.StatusOK, gin.H{
		"code": "200",
		"data": map[string]interface{}{
			"data": "图片上传成功",
			"msg":  file,
		},
	})
}

/**
*	多文件上传
 */
func MulityUpload(c *gin.Context) {
	// Multipart form
	form, _ := c.MultipartForm()
	files := form.File["files"]
	log.Println(form)
	for _, file := range files {
		dst := "./assets/images/" + file.Filename
		// 上传文件至指定目录
		c.SaveUploadedFile(file, dst)
	}
	c.JSON(http.StatusOK, gin.H{
		"code": "200",
		"data": map[string]interface{}{
			"data": "图片上传成功",
			"msg":  files,
			"form": form,
		},
	})
}
